package com.kzx.blog.controller;

import com.kzx.blog.model.Blog;
import com.kzx.blog.model.Token;
import com.kzx.blog.service.IBlog;
import com.kzx.blog.service.IUser;
import com.kzx.blog.service.model.BlogAddParam;
import com.kzx.blog.service.model.BlogPagingParam;
import com.kzx.blog.service.model.BlogUpdateParam;
import com.kzx.blog.util.PageUtils;
import com.kzx.blog.util.ParseHttp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author kongz
 * @version 1.0 2021/11/29 10:42
 * @description 博客Controller
 */
@RestController
@RequestMapping("/blog")
@Api(value = "BlogController", tags = {"博客接口"})
public class BlogController {

    private final IBlog iBlog;

    private final IUser iUser;

    public BlogController(IBlog iBlog, IUser iUser) {
        this.iBlog = iBlog;
        this.iUser = iUser;
    }

    @PostMapping("/paging")
    @ApiOperation(value = "获取博客信息",
            notes = "【参数说明】\n" +
                    "pageIndex: 页码，-1查询全部\n" +
                    "pageSize: 页面大小，-1查询全部\n" +
                    "author: 作者编号，0或者不传查询全部, -1则根据登录的用户查询\n" +
                    "title: 标题，模糊查询，不传或者为空查询全部\n" +
                    "status: 状态，-1查询全部，0未审核，1未通过，2已通过\n" +
                    "type: 类型，-1查询全部，0原创，1转载\n" +
                    "top: 1置顶, 0不置顶, -1查询全部\n" +
                    "【结果说明】\n" +
                    "id: 编号\n" +
                    "title: 标题\n" +
                    "author: 作者编号\n" +
                    "authorName: 作者昵称\n" +
                    "content: 正文\n" +
                    "createdTime: 创建时间\n" +
                    "updateTime: 更新时间\n" +
                    "status: 状态 0未审核，1未通过，2已通过\n" +
                    "type: 类型 0原创，1转载\n" +
                    "srcUrl: 来源地址")
    public PageUtils<Blog> getBlogs(@RequestBody @Validated BlogPagingParam param, HttpServletRequest request) {
        if (param.getAuthor() == -1) {
            String token = ParseHttp.getHttpAccessToken(request);
            Token user = iUser.getToken(token);
            param.setAuthor(user.getUid());
        }
        return iBlog.getBlogs(param);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取博客信息")
    public Blog getBlog(@PathVariable("id") int id) {
        return iBlog.getBlog(id);
    }

    @PatchMapping("")
    @ApiOperation("根据id更新博客内容")
    public int updateBlog(@RequestBody @Validated BlogUpdateParam param, HttpServletRequest request) {
        String token = ParseHttp.getHttpAccessToken(request);
        Token user = iUser.getToken(token);
        return iBlog.update(param, user);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除博客")
    public int deleteBlog(@PathVariable("id") int blogId, HttpServletRequest request) {
        String token = ParseHttp.getHttpAccessToken(request);
        Token user = iUser.getToken(token);
        return iBlog.delete(blogId, user);
    }

    @PatchMapping("/top/{id}")
    @ApiOperation("根据id置顶博客")
    public int topBlog(@PathVariable("id") int blogId, HttpServletRequest request) {
        String token = ParseHttp.getHttpAccessToken(request);
        Token user = iUser.getToken(token);
        return iBlog.top(blogId, user);
    }

    @PostMapping("")
    @ApiOperation("新建博客")
    public int addBlog(@RequestBody @Validated BlogAddParam blogAddParam, HttpServletRequest request) {
        String token = ParseHttp.getHttpAccessToken(request);
        Token user = iUser.getToken(token);
        return iBlog.add(blogAddParam, user);
    }


}
